package com.lintf.bot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.bot.domain.BotNotice;
import com.lintf.bot.domain.dto.BotNoticeAddDTO;
import com.lintf.bot.domain.dto.BotNoticeListDTO;
import com.lintf.bot.domain.dto.BotNoticeUpdateDTO;
import com.lintf.bot.domain.vo.BotNoticeListVo;
import com.lintf.bot.mapper.BotNoticeMapper;
import com.lintf.bot.service.IBotNoticeService;
import com.lintf.common.utils.SecurityUtils;
import com.lintf.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lintf
 * @description 针对表【bot_notice】的数据库操作Service实现
 * @createDate 2024-03-18 19:47:41
 */
@Service
public class BotNoticeServiceImpl extends ServiceImpl<BotNoticeMapper, BotNotice>
        implements IBotNoticeService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public List<BotNoticeListVo> list(BotNoticeListDTO dto) {
        LambdaQueryWrapper<BotNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BotNotice::getSystemNotice, dto.getSystemNotice())
                .orderByDesc(BotNotice::getCreateTime);
        if (dto.getSystemNotice() == 1) {
            wrapper.eq(BotNotice::getToUserId, SecurityUtils.getUserId());
        }else{
            wrapper.eq(BotNotice::getGroupId, dto.getGroupId());
        }
        List<BotNoticeListVo> list = new ArrayList<>();
        BotNoticeListVo vo;
        for (BotNotice notice : baseMapper.selectList(wrapper)) {
            vo = new BotNoticeListVo();
            BeanUtils.copyProperties(notice, vo);
            vo.setSysUser(sysUserService.selectUserById(Long.valueOf(notice.getCreateBy())));
            list.add(vo);
        }
        return list;
    }

    @Override
    public void add(BotNoticeAddDTO dto) {
        BotNotice notice = new BotNotice();
        BeanUtils.copyProperties(dto, notice);
//        notice.setCreateParams();
        baseMapper.insert(notice);
    }

    @Override
    public void update(BotNoticeUpdateDTO dto) {
        BotNotice notice = new BotNotice();
        BeanUtils.copyProperties(dto, notice);
//        notice.setUpdateParams();
        baseMapper.updateById(notice);
    }

    @Override
    public void remove(BotNoticeUpdateDTO dto) {
        BotNotice notice = new BotNotice();
        BeanUtils.copyProperties(dto, notice);
//        notice.setUpdateParams();
        baseMapper.deleteById(notice);
    }
}




